package config

import (
	"app/env"
	"encoding/json"
	"github.com/fwhezfwhez/errorx"
	_ "github.com/go-sql-driver/mysql"
	"github.com/sirupsen/logrus"
	_ "go.elastic.co/apm"
	"os"
	"time"
)

// Config 配置数据
type config struct {
	Permission []map[string]interface{} `json:"permission"`
	Menu       []map[string]interface{} `json:"menu"`
}

var Cfg config

func init() {
	data, err := os.ReadFile(env.BaseDir + "/config/permission.json")
	if err != nil {
		panic(errorx.Wrap(err))
	}

	Cfg = config{}
	err = json.Unmarshal(data, &Cfg)
	if err != nil {
		panic(errorx.Wrap(err))
	}

	logrus.Info("system config loaded")
}

func Reload(fileName string) {
	time.Sleep(1)
	data, err := os.ReadFile(env.BaseDir + "/config/permission.json")
	if err != nil {
		logrus.Error(errorx.Wrap(err))
		return
	}

	newCfg := config{}
	err = json.Unmarshal(data, &newCfg)
	if err != nil {
		logrus.Error(errorx.Wrap(err))
		return
	}

	Cfg = newCfg
	logrus.Info("system config reloaded")
}
